package com.ansatsing.strategy;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;//此类非彼类【org.slf4j.Logger】
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
@RunWith(Parameterized.class)
public class DragonSlayingStrategyTest2 {
	private InMemoryAppender appender;
	private DragonSlayingStrategy strategy;
	private String expectedResult;
	
	public DragonSlayingStrategyTest2(DragonSlayingStrategy strategy, String expectedResult) {
		this.strategy = strategy;
		this.expectedResult = expectedResult;
	}
	/**
	 * 产生构造函数的参数
	 * @return
	 */
	@Parameterized.Parameters
	public static Collection<Object[]> DataSource(){
	    return Arrays.asList(
	            new Object[]{
	                new MeleeStrategy(),
	                "With your Excalibur you sever the dragon's head!"
	            },
	            new Object[]{
	                new ProjectileStrategy(),
	                "You shoot the dragon with the magical crossbow and it falls dead on the ground!"
	            },
	            new Object[]{
	                new SpellStrategy(),
	                "You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"
	            }
	        );
	}

	@Before
	public void setUp() throws Exception {
		appender = new InMemoryAppender();
	}

	@After
	public void tearDown() throws Exception {
		appender.stop();
	}

	@Test
	public void testExecute() {
		this.strategy.execute();
		System.out.println(">>>>>>>>>>>>:"+appender.getLastMessage());
		assertEquals(this.expectedResult, appender.getLastMessage());
	    assertEquals(1, appender.getLogSize());
	}
	private class InMemoryAppender extends AppenderBase<ILoggingEvent>{
		private List<ILoggingEvent> log = new LinkedList<ILoggingEvent>();
		
		public InMemoryAppender() {
			 ((Logger) LoggerFactory.getLogger("root")).addAppender(this);
		      start();
		}

		@Override
		protected void append(ILoggingEvent arg0) {
			log.add(arg0);
		}
		public int getLogSize(){
			return log.size();
		}
		public String getLastMessage(){
			return log.get(log.size() -1).getFormattedMessage();
		}
	}
}
